package Exp2_ChenGY;

import java.util.ArrayList;  
import java.util.Collections;  
  
// 评委分数输入类  
class InputScore {  
    private ArrayList<Integer> scores;  
  
    public InputScore() {  
        scores = new ArrayList<>();  
    }  
  
    public void addScore(int score) {  
        if (score >= 0 && score <= 100) { // 假设分数是0-100之间的整数  
            scores.add(score);  
        } else {  
            throw new IllegalArgumentException("Score must be between 0 and 100");  
        }  
    }  
  
    public ArrayList<Integer> getScores() {  
        return scores;  
    }  
}  
  
// 去掉最高分和最低分类  
class DelScore {  
    private ArrayList<Integer> processedScores;  
  
    public DelScore(ArrayList<Integer> scores) {  
        if (scores == null || scores.size() < 3) {  
            throw new IllegalArgumentException("Not enough scores to remove the highest and lowest");  
        }  
        processedScores = new ArrayList<>(scores);  
        Collections.sort(processedScores);  
        processedScores.remove(0); // 去掉最低分  
        processedScores.remove(processedScores.size() - 1); // 去掉最高分  
    }  
  
    public ArrayList<Integer> getProcessedScores() {  
        return processedScores;  
    }  
}  
  
// 计算平均值类  
class ComputerAver {  
    public double computeAverage(ArrayList<Integer> scores) {  
        if (scores == null || scores.isEmpty()) {  
            throw new IllegalArgumentException("No scores to compute average");  
        }  
        double sum = 0;  
        for (int score : scores) {  
            sum += score;  
        }  
        return sum / scores.size();  
    }  
}  
  
// 流水线类  
class Line {  
    private InputScore inputScore;  
    private DelScore delScore;  
    private ComputerAver computerAver;  
  
    public Line() {  
        inputScore = new InputScore();  
        delScore = null; // 初始化时不直接创建DelScore实例  
        computerAver = new ComputerAver();  
    }  
  
    public void addScore(int score) {  
        inputScore.addScore(score);  
    }  
  
    public double getFinalScore() {  
        if (delScore == null) {  
            // 首次计算最终得分时创建DelScore实例  
            delScore = new DelScore(inputScore.getScores());  
        }  
        return computerAver.computeAverage(delScore.getProcessedScores());  
    }  
}  
  
// 示例程序  
public class ScorePipeLineDemo {  
    public static void main(String[] args) {  
        Line line = new Line();  
        line.addScore(90);  
        line.addScore(85);  
        line.addScore(95);  
        line.addScore(78);  
        line.addScore(100); // 假设这是错误的分数，但由于InputScore没有校验，这里仍然会添加  
  
        try {  
            double finalScore = line.getFinalScore();  
            System.out.println("Final Score: " + finalScore);  
        } catch (IllegalArgumentException e) {  
            System.out.println("Error occurred: " + e.getMessage());  
        }  
    }  
}